package zpe.relatorio.web;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zpe.relatorio.core.FacadeReport;
import zpe.relatorio.core.ReportCommand;
import zpe.relatorio.core.ReportCommandFactory;
import zpe.relatorio.model.Extensao;

public class ReportServlet extends HttpServlet {

	private static final long serialVersionUID = 3218132505799968044L;
	private FacadeReport facadeReport;
	private ReportCommandFactory cmdFactory;

	@Override
	public void init() throws ServletException {
		//Instancia o facade que executar� os relat�rios.
		facadeReport = new FacadeReport();
		//Cria uma �nica inst�ncia do factory de relat�rios.
		cmdFactory = ReportCommandFactory.getInstance();
	}
	
	private int parameter;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//Instancia o command que cont�m os par�metros para o relat�rio.  
		ReportCommand reportCmd =  cmdFactory.getCommand(req.getParameter("report"));
		OutputStream out        =  (OutputStream) resp.getOutputStream();
		//Pega os par�metros para o relat�rio.
		Map<String,Object> map  =  reportCmd.getParameters(req);
		//Formato de relat�rio informado pelo usu�rio: html ou pdf
		String formato = req.getParameter( "formato" );
		
		if( formato.equalsIgnoreCase( Extensao.PDF.name() ) ){
			resp.setHeader("Content-Disposition"," inline; filename=Report" + parameter++ + ".pdf");
			resp.setContentType("application/pdf");
			facadeReport.executePdf( out, reportCmd.getReport(),map );
		}else if( formato.equalsIgnoreCase( Extensao.HTML.name() ) ){
			resp.setHeader("Content-Disposition"," inline; filename=Report" + parameter++ + ".html");
			resp.setContentType("text/html");
			ServletContext ctx = req.getSession(false).getServletContext();
			//Imagens ficar�o no diret�rio raiz da aplica��o 
			String dest = ctx.getRealPath("/");
			facadeReport.executeHtml( out, reportCmd.getReport(), req.getParameter("report"), dest, map );
		}else if( formato.equalsIgnoreCase( Extensao.XLS.name() ) ){
			resp.setHeader("Content-Disposition"," inline; filename=Report" + parameter++ + ".xls");
			resp.setContentType("application/vnd.ms-excel");
			facadeReport.executeXls( out, reportCmd.getReport(),map );
		}else if( formato.equalsIgnoreCase( Extensao.ODT.name() ) ){
            resp.setHeader("Content-Disposition"," inline; filename=Report"+ parameter++ + ".odt");
            resp.setContentType("application/vnd.oasis.opendocument.text");
            facadeReport.executeODT( out, reportCmd.getReport(),map );          
        }
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

			doPost(req, resp);
	}
}
